
;
(function($, $$) {
    $.fn.menu = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.Menu);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.Menu = function(self, options) {
        if (!(this instanceof $$.Menu)) {
            return new $$.Menu(self, options);
        }
        var defaults = {

            topMenuClass : $$.defaultGradientBG,

            subMenuClass : $$.defaultGradientBG + " uiCornerAll " + $$.defaultBorder,

            hoverClass : $$.hoverGradientBG
        };
        var opts = $.extend(defaults, options);
        $$.Menu.prototype.superClass.call(this, self, opts);
        this.init();
    }
    $$.inherit($$.Menu, $$.Widget, {

        init : function() {
            var menuObj = this;
            menuObj.createWidget();
            initMenu(menuObj.jqObj, menuObj.opts);
            return menuObj;
        }
    });

    function initMenu(self, opts) {
        self.addClass("uiMenu uiWidget " + opts.topMenuClass).children("ul").addClass("uiTopLevel")
            .children("li:not(:last)").addClass("rightBorder").end()
            .find("ul").addClass("uiSubLevel " + opts.subMenuClass).children("li:not(::last-child)").children("a").addClass("bottomBorder");
        var topMenuWidth = 0, topMenuObj = self.children("ul");
        $("> li", topMenuObj).each(function() {
            topMenuWidth += $(this).outerWidth(true);
        });
        topMenuObj.width(topMenuWidth);
        initEvent(self, opts);
    }


    function initEvent(self, opts) {
        self.find("li").hover(function() {
            $(this).addClass(opts.hoverClass);
        }, function() {
            $(this).removeClass(opts.hoverClass);
        });
    }
})(jQuery, BASE);